<template>
  <div>
    <div class="goods-list-back">
         <div @click="bgoBack">
            <van-icon name="arrow-left" color='#fff' size="20px"/>
          </div>
      </div>
    <div ref="goodsInfo" class="goodsInfo">
      <section class="goods-info">
        <article>
          <mt-swipe :auto="3000" :show-indicators="false">
              <mt-swipe-item v-for="item in goodsinfo.pics" :key="item.pics_id">
                <img :src="item.pics_mid" alt="">
              </mt-swipe-item>
          </mt-swipe>
          <div class="goodsinfo-title">
            <h2 class="info-price">￥{{goodsinfo.goods_price}}</h2>
            <p class="info-name">{{goodsinfo.goods_name}}</p>
          </div>
          <div v-html="goodsinfo.goods_introduce"></div>
        </article>
      </section>
    </div>
    <goods-button @addToCart='addToCart'></goods-button>
  </div>
</template>

<script>
import BScroll from 'better-scroll'
import GoodsButton from './GoodsButton.vue'
import { getDetail } from '@/network/search.js'
export default {
  name: 'GoodsInfo',
  data () {
    return {
      goodsinfo: {}, // 获取到的商品信息
      goods_id: this.$route.query.goods_id
    }
  },
  components: {
    GoodsButton
  },
  // props: ['goods_id'],
  created () {
    this.getGoodsInfo()
    console.log(this.$route.query.goods_id)
  },
  mounted () {
    this.getBscoll()
  },
  methods: {
    getGoodsInfo () {
      getDetail(this.goods_id).then(res => {
        console.log(res.message)
        this.goodsinfo = res.message
      })
    },
    getBscoll () {
      this.$nextTick(() => {
        this.scroll = new BScroll(this.$refs.goodsInfo, {})
      })
    },
    addToCart () {
      const product = {}
      product.name = this.goodsinfo.goods_name
      product.image = this.goodsinfo.goods_small_logo
      product.id = this.goodsinfo.goods_id
      product.price = this.goodsinfo.goods_price
      product.checked = false
      this.$store.dispatch('addCart', product)
      this.$toast.show('添加购物车成功', 2000)
    },
    bgoBack () {
      this.$router.go(-1)
    }
  }
}
</script>

<style lang="scss" scoped>
  .goods-list-back{
      position: fixed;
      top: 1rem;
      left: 1rem;
      height: 40px;
      width: 40px;
      background-color: rgba(0, 0, 0,.4);
      border-radius: 50%;
      z-index: 999;
      line-height: 40px;
      text-align: center;
  }
  .mint-swipe{
    width: 100vw;
    height: 400px;
    img{
      width: 100%;
      height: 100%;
    }
  }
  .goodsInfo{
    overflow: hidden;
    position: absolute;
    top: 0px;
    bottom: 51px;
  }
  .goodsinfo-title{
    background: rgba(255, 245, 249, 0.1);
    box-shadow: 0px 2px 2px rgba(0, 0, 0,.1);
    margin-top: .1rem;
    margin-bottom: 20px;
    padding: .5rem;
    border-radius: 0px 0px .5rem .5rem;
    .info-price{
      padding-left: .5rem;
      padding-bottom: .5rem;
      color: red;
    }
    .info-name{
      color: #000;
      font-weight: bold;
      padding-left: .5rem;
    }
  }

</style>
